package com.hz.springboot02.exception;

import com.hz.springboot02.utils.MessageJson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @ClassName GlobalException
 * @Description 处理全局异常
 * @Author Administrator
 * @Date 2024/12/10 17:15
 */
@ControllerAdvice(basePackages = "com.hz.springboot02")
@Slf4j

public class GlobalException {
    /**
     * 监听全局异常
     * @param e
     * @return
     */
    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public MessageJson handlerException(RuntimeException e){
        log.error("系统异常：",e);
        return MessageJson.error(400,"请求失败");
    }

    /**
     * 监听自定义异常
     * @param e
     * @return
     */
    @ExceptionHandler(SmbmsException.class)
    @ResponseBody
    public MessageJson handlerException(SmbmsException e){
        log.error("系统异常：",e);
        return MessageJson.error(400,"请求失败");
    }
}
